from django import template
from django.utils.html import escape
from django.utils.timesince import timesince
import datetime
#from pygments import highlight as highlight_handler
#from pygments.lexers.text import RstLexer
#from pygments.lexers import get_lexer_by_name
#from pygments.formatters import HtmlFormatter
#from difflib import HtmlDiff

register = template.Library()

@register.filter
def linediff(value, arg):
    value_set = set(value.split('\n'))
    arg_set = set(arg.split('\n'))
    only_in_value = value_set - arg_set
    result = []
    for line in value.split('\n'):
        if line in only_in_value:
            line = '<span style = "background-color:#E1E1E1">%s</span>' % escape(line)
        else:
            line = escape(line)
        result.append(line)
    return "\n".join(result)

#    d = HtmlDiff()
#    return d.make_table(value.split('\n'), arg.split('\n'))

#@register.filter
#def highlight(value, arg = None):
#    if arg:
#        lexer = get_lexer_by_name(arg)
#    else:
#        lexer = RstLexer()
#    return highlight_handler(value, lexer, HtmlFormatter())

@register.filter
def ago(value):
    today = datetime.date.today()
    value_date = value.date()
    delta = today - value_date
    if delta.days == 0:
        return timesince(value) + " ago"
    elif delta.days == 1:
        return u"Yesterday"
    elif delta.days >= 2:
        return u"%s days ago" % (delta.days,)
    return value.strftime("%B %d, %Y")